const Koa = require('koa');
const serve = require('koa-static');
const koaBody = require('koa-body');
const app = new Koa();
const router = require('./routes/index');
require('./lib/bootstrap-spider');

const { connection, connect } = require('mongoose');

connect('mongodb://localhost:27017/tvapp', { useNewUrlParser: true, useCreateIndex: true });

connection.on('error', console.error.bind(console, 'connection error:'));

connection.once('open', async () => {
    app.use(koaBody());
    app.use(serve(__dirname + '/uploads'));

    app.use(router.routes()).use(router.allowedMethods());
    app.listen(process.env.PORT || 80);
});
